package com.fzk.blog.mapper;

import org.apache.ibatis.builder.annotation.ProviderMethodResolver;
import org.apache.ibatis.jdbc.SQL;

/**
 * @author fzk
 * @date 2021-08-23 17:45
 * 提供动态的SQL
 */
public class CommentSqlProvider implements ProviderMethodResolver {
    public static String getCommentInfoListPage(Integer blogId, Integer userId, Integer index, Integer pageSize) {
        return new SQL() {{
            SELECT("id,user_id userId,blog_id blogId,content,publish_date publishDate,create_time createTime");
            FROM("tbl_comment");
            WHERE("flag=0");
            if (userId != null) {
                WHERE("user_id=#{userId}");
            }
            if (blogId != null) {
                WHERE("blog_id=#{blogId}");
            }
            ORDER_BY("blogId ASC,createTime DESC,userId ASC");
            if (index != null) {
                LIMIT("#{pageSize}");
            }
            if (pageSize != null) {
                OFFSET("#{index}");
            }
        }}.toString();
    }

    public static String getCommentInfoTotal(Integer blogId, Integer userId, Integer index, Integer pageSize){
        return new SQL(){{
            SELECT("COUNT(*)");
            FROM("tbl_comment");
            WHERE("flag=0");
            if (userId != null) {
                WHERE("user_id=#{userId}");
            }
            if (blogId != null) {
                WHERE("blog_id=#{blogId}");
            }
        }}.toString();
    }
}
